package com.example.pkmedical.Activity.weight.weightdevice;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Activity.weight.history.WeightLineChartActivity;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SPUtils;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WeightDeviceActivity extends BaseActivity {

    @BindView(R.id.weightdevice_applay_one)
    TextView weightdeviceApplayOne;
    @BindView(R.id.weightdevice_time_one)
    TextView weightdeviceTimeOne;
    @BindView(R.id.weightdevice_applay_two)
    TextView weightdeviceApplayTwo;
    @BindView(R.id.weightdevice_time_two)
    TextView weightdeviceTimeTwo;
    @BindView(R.id.weightdevice_bmi)
    TextView weightdeviceBmi;
    @BindView(R.id.weightdevice_height)
    TextView weightdeviceHeight;
    @BindView(R.id.weightdevice_history)
    Button weightdeviceHistory;
    @BindView(R.id.weightdevice_weighing)
    Button weightdeviceWeighing;
    @BindView(R.id.weightdevice_goback)
    Button weightdeviceGoback;
    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;
    @BindView(R.id.weightdevice_height_image)
    ImageView weightdeviceHeightImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weightdevice);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        DecimalFormat df = new DecimalFormat("#.00");
        Log.d("yangshuoWeight100", SPUtils.getInstance().getString("user_difference_weight_all"));
        Log.d("yangshuoWeight200", SPUtils.getInstance().getString("user_first_weight"));
        Log.d("yangshuoWeight300", SPUtils.getInstance().getString("user_now_weight"));
        if (0 == CommandUtil.COMPANY_TYPE) {
            if (SPUtils.getInstance().getString("user_first_weight").equals("不适用") || SPUtils.getInstance().getString("user_now_weight").equals("不适用")) {
                weightdeviceApplayOne.setText(SPUtils.getInstance().getString("user_first_weight"));
                weightdeviceApplayTwo.setText(SPUtils.getInstance().getString("user_now_weight"));
            } else {
                if ("0".equals(SPUtils.getInstance().getString("user_difference_weight_all"))) {
                    weightdeviceApplayTwo.setText(SPUtils.getInstance().getString("user_now_weight") + " KG");
                } else {
                    weightdeviceApplayTwo.setText(df.format(Double.parseDouble(SPUtils.getInstance().getString("user_now_weight")) - Double.parseDouble(SPUtils.getInstance().getString("user_difference_weight_all"))) + " KG");
                }
                weightdeviceApplayOne.setText(SPUtils.getInstance().getString("user_first_weight") + " KG");
            }
        } else {
            if (SPUtils.getInstance().getString("user_first_weight").equals("不适用") || SPUtils.getInstance().getString("user_now_weight").equals("不适用")) {
                weightdeviceApplayOne.setText(SPUtils.getInstance().getString("user_first_weight"));
                weightdeviceApplayTwo.setText(SPUtils.getInstance().getString("user_now_weight"));
            } else {
                weightdeviceApplayOne.setText(Float.parseFloat(SPUtils.getInstance().getString("user_first_weight")) * 2 + " LB");
                if ("0".equals(SPUtils.getInstance().getString("user_difference_weight_all"))) {
                    weightdeviceApplayTwo.setText(Float.parseFloat(SPUtils.getInstance().getString("user_now_weight")) * 2 + " LB");
                } else {
                    float mWeight = Float.parseFloat(SPUtils.getInstance().getString("user_now_weight")) - Float.parseFloat(SPUtils.getInstance().getString("user_difference_weight_all"));
                    weightdeviceApplayTwo.setText(df.format(mWeight * 2) + " LB");
                }
            }
        }
        if (0 == CommandUtil.HEIGHT_COMPANY_TYPE) {

            if (SPUtils.getInstance().getString("user_height").equals("不适用")) {
                weightdeviceHeight.setText(getText(R.string.weight_device_height_no_apply) + "  " + SPUtils.getInstance().getString("user_height"));
                weightdeviceBmi.setText(getText(R.string.weight_device_BMI_no_apply) + "  " + SPUtils.getInstance().getString("user_height"));
            } else {
                weightdeviceHeight.setText(getText(R.string.weight_device_height_no_apply) + "  " + df.format(Float.parseFloat(SPUtils.getInstance().getString("user_height"))) + " CM");
                weightdeviceBmi.setText(getText(R.string.weight_device_BMI_no_apply) + "  " + BMI());
            }
        } else {
            if (SPUtils.getInstance().getString("user_height").equals("不适用")) {
                weightdeviceHeight.setText(getText(R.string.weight_device_height_no_apply) + "  " + SPUtils.getInstance().getString("user_height"));
                weightdeviceBmi.setText(getText(R.string.weight_device_BMI_no_apply) + "  " + SPUtils.getInstance().getString("user_height"));
            } else {
//                weightdeviceHeight.setText(getText(R.string.weight_device_height_no_apply) + "  " +  df.format(Float.parseFloat(SPUtils.getInstance().getString("user_height")) * 0.4) + " in");
                weightdeviceHeight.setText(getText(R.string.weight_device_height_no_apply) + "  " + df.format(Float.parseFloat(SPUtils.getInstance().getString("user_height"))) + " in");
                weightdeviceBmi.setText(getText(R.string.weight_device_BMI_no_apply) + "  " + BMI());
            }
        }
        weightdeviceTimeOne.setText(SPUtils.getInstance().getString("Initial_time"));
        weightdeviceTimeTwo.setText(SPUtils.getInstance().getString("Last_time"));
    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_weight);
        mainHouseText.setText(R.string.title_weight);
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.weightdevice_history, R.id.weightdevice_weighing, R.id.weightdevice_goback, R.id.weightdevice_height_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.weightdevice_history:
                startActivity(new Intent(MyApp.getAppContext(), WeightLineChartActivity.class));
                break;
            case R.id.weightdevice_weighing:
                if (CommandUtil.HOUBEI_ISZERO && CommandUtil.CHUANGTI_ISZERO) {
                    startActivity(new Intent(MyApp.getAppContext(), WeightDeviceBedActivity.class));
                } else {
                    startActivity(new Intent(MyApp.getAppContext(), WeightCantStartActivity.class));
                }

//                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                break;
            case R.id.weightdevice_goback:
                finish();
                break;
            case R.id.weightdevice_height_image:
                startActivity(new Intent(MyApp.getAppContext(), UserHeightActivity.class));
//                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                break;
        }
    }

    private String BMI() {
        float mHeight;
        float mWeight;
        if (SPUtils.getInstance().getString("user_height").equals("不适用") || SPUtils.getInstance().getString("user_first_weight").equals("不适用")) {
            return "不适用";
        } else {
            if (0 == CommandUtil.HEIGHT_COMPANY_TYPE) {
                mHeight = Float.parseFloat(SPUtils.getInstance().getString("user_height")) / 100;
            } else {
                mHeight = Float.parseFloat(Float.parseFloat(SPUtils.getInstance().getString("user_height")) / 0.393+"") / 100;
            }

            mWeight = Float.parseFloat(SPUtils.getInstance().getString("user_now_weight"));
            float mBMI = mWeight / (mHeight * mHeight);
            DecimalFormat decimalFormat = new DecimalFormat("00.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
//            decimalFormat.format(mBMI);//format 返回的是字符串
            SPUtils.getInstance().put("BMI",decimalFormat.format(mBMI) + "");
            return decimalFormat.format(mBMI) + "";
        }
    }
}
